package com.aretias.jkholdings.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.aretias.jkholdings.model.User;
import com.aretias.jkholdings.service.UserService;

@Controller
public class UserController extends BaseController{
	
	@Autowired
	UserService userService;
		
	@RequestMapping(value = "/login.do" , method = RequestMethod.GET)
	public ModelAndView goLoginPage(){
		ModelAndView mav = new ModelAndView("login");
		mav.addObject("categoryList", getFirstDeptCategoryList());
		return mav;
	}
	
	@RequestMapping(value = "/login.do" , method = RequestMethod.POST)
	public ModelAndView login(User param, HttpSession session){
		
		param = userService.getUser(param);
		
		ModelAndView mav = new ModelAndView();
		
		if(getUserForSession(session) != null){
			session.setAttribute(key, param);
			mav.setViewName("redirect:/home");
		}else if(param == null){
			mav.setViewName("redirect:/login.do");
		}else{
			session.setAttribute(key, param);
			mav.setViewName("redirect:/home.do");
		}
		return mav;
	}
}
